﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xyz.Framework.Server.DataAcessLayer;
using System.Data.Objects;
using Xyz.Hrs.Shared.DataContracts.DataObjects;
using System.Transactions;
using Xyz.Hrs.Server.DomainObjects;

namespace Xyz.Hrs.Server.HrsManagementBL
{
    public class PatientManagement
    {
        public const string CONNECTION_STRING = "name = HRSDomainEntities";
        public const string CONTAINER_NAME = "HRSDomainEntities";
        public static PatientDTO[] SavePatient(PatientDTO[] patients)
        {
            if (patients == null || patients.Length == 0)
                return null;
            using (TransactionScope trans = new TransactionScope())
            {
                using (DataBaseContext db = new DataBaseContext(CONNECTION_STRING, CONTAINER_NAME))
                {
                    ObjectSet<HrsPerson> _personSet = db.CreateObjectSet<HrsPerson>();
                    ObjectSet<Patient> _patientSet = db.CreateObjectSet<Patient>();

                    int[] _patientIds = patients.Select(x => x.PatientId).ToArray();

                    var _savedDatas = (from x in _patientSet
                                       join y in _personSet on x.PersonId equals y.Id
                                       where _patientIds.Contains(x.Id)
                                       select new
                                       {
                                           person = y,
                                           patient = x
                                       }).ToArray();


                    foreach (var patientDTOItem in patients)
                    {
                        bool _newdata = false;
                        Patient _patient = null;
                        HrsPerson _person = null;
                        var _result = _savedDatas.SingleOrDefault(s => s.patient.Id == patientDTOItem.PatientId);
                        if (_result == null)
                        {
                            _person = new HrsPerson();
                            _patient = new Patient();
                            _personSet.AddObject(_person);
                            _patientSet.AddObject(_patient);
                            _newdata = true;
                        }
                        else
                        {
                            _person = _result.person;
                            _patient = _result.patient;
                        }

                        _person.IdentityNo = patientDTOItem.IdentityNo;
                        _person.Name = patientDTOItem.Name;
                        _person.Surname = patientDTOItem.Surname;
                        _person.BirthDay = patientDTOItem.BirthDate;
                        _person.BirthPlace = patientDTOItem.BirthPlace;

                        try
                        {
                            db.SaveChanges();
                        }
                        catch (Exception ex)
                        {
                            if (ex.Message.Contains("UK_Identity"))
                                throw new Exception("Aynı tc kimlik numarasına sahip kişi kaydı oluşturulamaz");
                        }

                        _patient.PersonId = _person.Id;
                        db.SaveChanges();

                        if (_newdata)
                        {
                            patientDTOItem.PatientId = _patient.Id;
                        }
                    }
                }
                trans.Complete();
                return patients;
            }
        }
    }
}
